package com.xiaoshuidi.cloud.module.finance.controller.admin.invoice;

import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.hangxin.core.dto.InvoicingRespDTO;
import com.xiaoshuidi.cloud.module.finance.controller.admin.invoice.vo.FinanceInvoiceApprovalReqVO;
import com.xiaoshuidi.cloud.module.finance.controller.admin.invoice.vo.FinanceInvoiceRecordPageReqVO;
import com.xiaoshuidi.cloud.module.finance.controller.admin.invoice.vo.FinanceInvoiceRecordRespVO;
import com.xiaoshuidi.cloud.module.finance.controller.admin.invoice.vo.FinanceInvoiceRecordVO;
import com.xiaoshuidi.cloud.module.finance.convert.finance.InvoiceConvert;
import com.xiaoshuidi.cloud.module.finance.dal.dataobject.invoice.FinanceInvoiceRecordDO;
import com.xiaoshuidi.cloud.module.finance.dal.dataobject.invoice.FinanceInvoiceRecordItemDO;
import com.xiaoshuidi.cloud.module.finance.service.invoice.FinanceInvoiceRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Tag(name = "管理后台 - 发票")
@RestController
@RequestMapping("/finance/invoice/record")
public class FinanceInvoiceRecordController {
    @Resource
    private FinanceInvoiceRecordService financeInvoiceRecordService;

    @GetMapping("/page")
    @Operation(summary = "开票记录列表")
    @PreAuthorize("@ss.hasPermission('finance:invoice:record:query')")
    public CommonResult<PageResult<FinanceInvoiceRecordVO>> list(FinanceInvoiceRecordPageReqVO reqVO) {
        return CommonResult.success(InvoiceConvert.INSTANCE.convertToPage(financeInvoiceRecordService.getPage(reqVO)));
    }

    @GetMapping("/get")
    @Operation(summary = "开票记录详情")
    @PreAuthorize("@ss.hasPermission('finance:invoice:record:query')")
    public CommonResult<FinanceInvoiceRecordRespVO> get(@RequestParam("id") Long id) {
        FinanceInvoiceRecordDO record = financeInvoiceRecordService.getById(id);
        FinanceInvoiceRecordRespVO resp = InvoiceConvert.INSTANCE.convert(record);
        List<FinanceInvoiceRecordItemDO> items = financeInvoiceRecordService.getItems(record.getId());
        resp.setItems(items);
        return CommonResult.success(resp);
    }

    @PostMapping("/approval")
    @Operation(summary = "开票审核")
    @PreAuthorize("@ss.hasPermission('finance:invoice:record:approval')")
    public CommonResult<Void> approval(@RequestBody FinanceInvoiceApprovalReqVO reqVO) {
        financeInvoiceRecordService.approve(reqVO);
        return CommonResult.success();
    }

    @GetMapping("/query-result")
    @Operation(summary = "查询开票状态")
    @PreAuthorize("@ss.hasPermission('finance:invoice:record:query')")
    public CommonResult<String> query(@RequestParam("id") Long id){
        InvoicingRespDTO respDTO = financeInvoiceRecordService.query(id);
        return CommonResult.success(respDTO.getFP_STATUS());
    }

    @GetMapping("/resend")
    @Operation(summary = "重新请求开票")
    @PreAuthorize("@ss.hasPermission('finance:invoice:record:query')")
    public CommonResult<Void> test(@RequestParam("id") Long id) {
        FinanceInvoiceRecordDO record = financeInvoiceRecordService.getById(id);
        financeInvoiceRecordService.callApi(record);
        return CommonResult.success();
    }
}
